package com.bishe.psy.service.impl;

import com.bishe.psy.common.AppResult;
import com.bishe.psy.common.ResultCode;
import com.bishe.psy.dao.MessageMapper;
import com.bishe.psy.exception.ApplicationException;
import com.bishe.psy.model.Message;
import com.bishe.psy.service.IMessageService;
import com.bishe.psy.utils.StringUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: L
 * Date: 2024-04-30
 * Time: 12:18
 */

@Slf4j
@Service
public class MessageServiceImpl implements IMessageService {

    @Resource
    private MessageMapper messageMapper;


    @Override
    public void create(Message message) {

        //1.参数校验
        if(message == null || message.getPostUserId() == null || message.getReceiveUserId() == null
        || StringUtil.isEmpty(message.getContent()) || message.getCurrentIdentity() == null) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.设置默认值
        message.setState((byte) 0);
        message.setDeleteState((byte) 0);
        Date date = new Date();
        message.setCreateState(date);
        message.setUpdateState(date);

        //3.调用数据库
        int row = messageMapper.insertSelective(message);
        if(row != 1) {
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }

    }

    @Override
    public Integer selectUnreadCount(Long receiveUserId,Long currentIdentity) {

        //1.参数校验
        if(receiveUserId == null || receiveUserId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.调用数据库
        Integer count = messageMapper.selectUnreadCount(receiveUserId,currentIdentity);

        //3.返回结果
        return count;

    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {

        //1.参数校验
        if(receiveUserId == null || receiveUserId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.调用数据库
        List<Message> messages = messageMapper.selectByReceiveUserId(receiveUserId);

        //3.返回结果
        return messages;

    }

    @Override
    public List<Message> selectByReceiveUserIdStu(Long receiveUserId) {
        //1.参数校验
        if(receiveUserId == null || receiveUserId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.调用数据库
        List<Message> messages = messageMapper.selectByReceiveUserIdStu(receiveUserId);

        //3.返回结果
        return messages;
    }

    @Override
    public void updateStateById(Long id, Byte state) {

        //1.参数校验
        if(id == null || id <= 0 || state < 0 || state > 2) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.构造更新对象
        Message updateMessage = new Message();
        updateMessage.setId(id);
        updateMessage.setState(state);
        updateMessage.setUpdateState(new Date());

        //3.调用数据库
        int row = messageMapper.updateByPrimaryKeySelective(updateMessage);
        if(row != 1) {
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }

    }

    @Override
    public Message selectByPrimaryKey(Long id) {

        //1.参数校验
        if(id == null || id <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.调用数据库
        Message message = messageMapper.selectByPrimaryKey(id);

        //3.返回结果
        return message;

    }

    @Override
    public void reply(Long replyId, Message message) {

        //1.参数校验
        if(replyId == null || replyId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.查询站内信状态
        Message selectMessage = messageMapper.selectByPrimaryKey(replyId);
        if(selectMessage == null || selectMessage.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }

        //3.更新状态为已回复
        updateStateById(replyId, (byte) 2);

        //4.回复的内容写入数据库
        create(message);

    }
}
